package com.starsoft.trade.dao;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.starsoft.frame.core.orm.BaseDao;
import com.starsoft.frame.util.DateUtil;
import com.starsoft.trade.bean.TradeSummary;
import com.starsoft.trade.bean.TradeSummaryId;

@Repository
public class TradeSummaryDao extends BaseDao<TradeSummary, TradeSummaryId> {

	public Date getLastSummaryDate(Integer acountId, Date date){
		String hql = "select max(id.asofdate) from TradeSummary where  id.acountId=?0 and id.asofdate<?1 ";
		return (Date) this.getValue(hql, acountId, date);
	}
	
	public List<TradeSummary> getAcountAll(Integer acountId){
		String hql = "from TradeSummary where id.acountId = ?0 order by id.asofdate asc";
		return this.find(hql, acountId);
	}
	
	public List<TradeSummary> getAcountBefore(Integer acountId, Date beforeDate){
		String hql = "from TradeSummary where id.acountId = ?0 and id.asofdate<=?1 order by id.asofdate asc";
		return this.find(hql, acountId, beforeDate);
	}
	
	public List<TradeSummary> getAcountAfter(Integer acountId, Date afterDate){
		String hql = "from TradeSummary where id.acountId = ?0 and id.asofdate>=?1 order by id.asofdate asc";
		return this.find(hql, acountId, afterDate);
	}
	
	public List<TradeSummary> getAcountBetween(Integer acountId, Date beginDate, Date endDate){
		String hql = "from TradeSummary where id.acountId = ?0 and id.asofdate>=?1 and id.asofdate<=?2 order by id.asofdate asc";
		return this.find(hql, acountId, beginDate, endDate);
	}
	
	public int deleteAcountAll(Integer acountId){
		String hql = "delete from TradeSummary where  id.acountId = ?0";
		return update(hql, acountId);
	}
	
	public int deleteStatusAfter(Integer acountId, Date date) {
		String hql = "delete from TradeSummary where  id.acountId = ?0 and id.asofdate>=?1";
		return update(hql, acountId, DateUtil.floorTime(date));
	}
}
